<template>
  <el-container class="main-container">
    <!-- 头部区域 -->
    <el-header>
      <!-- 左侧的 logo -->
      <img src="../assets/images/logo.png" alt="" />
      <!-- 右侧的菜单 -->
      <el-menu class="el-menu-top" mode="horizontal" router :default-active="$route.path" background-color="#23262E" text-color="#fff" active-text-color="#409EFF">
        <el-submenu index="1">
          <template slot="title">
            <!-- 头像 -->
            <!-- <img :src="userInfo.user_pic?userInfo.user_pic:defalutAwatar" alt="" class="avatar" /> -->
            <img :src="userInfo.user_pic" alt="" v-if="userInfo.user_pic" class="avatar">
            <img src="../assets/logo.png" alt="" v-else class="avatar">
            <span>个人中心</span>
          </template>
          <el-menu-item index="/user-info"><i class="el-icon-s-operation"></i>基本资料</el-menu-item>
          <el-menu-item index="/user-avatar"><i class="el-icon-camera"></i>更换头像</el-menu-item>
          <el-menu-item index="/user-pwd"><i class="el-icon-key"></i>重置密码</el-menu-item>
        </el-submenu>
        <el-menu-item index="2" @click="out"><i class="el-icon-switch-button"></i>退出</el-menu-item>
      </el-menu>
    </el-header>
    <el-container>
      <!-- 侧边栏区域 -->
      <el-aside width="200px">
        <div class="user-box">
          <!-- <img :src="userInfo.user_pic?userInfo.user_pic:defalutAwatar" alt="" /> -->
          <img :src="userInfo.user_pic" alt="" v-if="userInfo.user_pic">
          <img src="../assets/logo.png" alt="" v-else>
          <span>欢迎 {{userInfo.nickname?userInfo.nickname: userInfo.username}}</span>
        </div>
        <!-- el-menu组件的router属性可以让菜单变成路由的链接 -->
        <!-- default-active是指高亮显示在哪个路由地址对应的模块 -->
        <!-- $route.path是指当前路径路由地址 -->
        <el-menu :default-active="$route.path" router class="el-menu-vertical-demo" background-color="#23262E" text-color="#fff" active-text-color="#409EFF" unique-opened>
          <!-- template标签上面不需要添加key,因为它不会被渲染 -->
          <template v-for="(item,n) in menus">
            <!-- 不包含子菜单的“一级菜单” -->
            <el-menu-item :key="n" :index="item.indexPath" v-if="!item.children"><i :class="item.icon"></i>{{item.title}}</el-menu-item>
            <!-- 包含子菜单的“一级菜单” -->
            <el-submenu :key="n" :index="item.indexPath" v-else>
              <template slot="title">
                <i :class="item.icon"></i>
                <span>{{item.title}}</span>
              </template>
              <el-menu-item v-for="(obj,i) in item.children" :key="i" :index="obj.indexPath"><i :class="obj.icon"></i>{{obj.title}}</el-menu-item>
            </el-submenu>
          </template>
        </el-menu>
      </el-aside>
      <el-container>
        <!-- 页面主体区域 -->
        <el-main>
          <router-view />
        </el-main>
        <!-- 底部 footer 区域 -->
        <el-footer>© www.itheima.com - 黑马程序员</el-footer>
      </el-container>
    </el-container>
  </el-container>
</template>

<script>
import { mapState, mapActions } from 'vuex'
// import defalutAwatar from '../assets/logo.png'
// 图片的路径不可以动态添加字符串形式的路径(把路径定义为变量并且导入图片路径即可)
// 也可以用v-if写两个img标签
export default {
  name: 'Main',
  data () {
    return {
      // defalutAwatar,
      menus: []
    }
  },
  computed: { ...mapState(['userInfo', 'token']) },
  methods: {
    ...mapActions(['getuserInfo']),
    // 退出登录
    out () {
      this.$confirm('确认退出?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        // 点击确定触发：清除token
        // 2. 清除token
        this.$store.commit('updateToken', '')
        // 3. 跳转到登录页面
        this.$router.push('/login')
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消退出'
        })
      })
    },
    async initMenus () {
      const res = await this.$axios.get('/my/menus', {
        headers: {
          Authorization: this.$store.state.token
        }
      })
      // console.log(res.data.data)
      if (res.data.code === 0) {
        this.menus = res.data.data
      }
    }

  },
  created () {
    if (this.token) {
      // 渲染用户信息
      // this.$store.dispatch('getuserInfo')
      this.getuserInfo()
      // 渲染左侧菜单栏
      this.initMenus()
    } else {
      // 如果 token 没有值，就强制跳转到登录页面
      this.$router.push('/login')
    }
  }
}
</script>

<style lang="less" scoped>
.main-container {
  height: 100%;
  .el-header,
  .el-aside {
    background-color: #23262e;
  }
  .el-header {
    padding: 0;
    display: flex;
    justify-content: space-between;
  }
  .el-main {
    overflow-y: scroll;
    height: 0;
    background-color: #f2f2f2;
  }
  .el-footer {
    background-color: #eee;
    font-size: 12px;
    display: flex;
    justify-content: center;
    align-items: center;
  }
}
// 左侧边栏用户信息区域
.user-box {
  height: 70px;
  display: flex;
  justify-content: center;
  align-items: center;
  border-top: 1px solid #000;
  border-bottom: 1px solid #000;
  user-select: none;
  img {
    width: 35px;
    height: 35px;
    border-radius: 50%;
    background-color: #fff;
    margin-right: 15px;
    object-fit: cover;
  }
  span {
    color: white;
    font-size: 12px;
  }
}
// 侧边栏菜单的样式
.el-aside {
  .el-submenu,
  .el-menu-item {
    width: 200px;
    user-select: none;
  }
}
.avatar {
  border-radius: 50%;
  width: 35px;
  height: 35px;
  background-color: #fff;
  margin-right: 10px;
  object-fit: cover;
}
</style>
